package com.deyuanyun.pic.ctrl.controller;

import com.deyuanyun.pic.common.dto.PageJsonBean;
import com.deyuanyun.pic.common.tool.JXL;
import com.deyuanyun.pic.common.util.ajax.AjaxSupport;
import com.deyuanyun.pic.common.util.poi.ExcelUtil;
import com.deyuanyun.pic.common.util.web.WebUtil;
import com.deyuanyun.pic.ctrl.controller.dto.InfluenceVO;
import com.deyuanyun.pic.ctrl.enumeration.InterferenceEnum;
import com.deyuanyun.pic.ctrl.service.InfluenceService;
import com.deyuanyun.pic.pipe.controller.BaseController;
import com.deyuanyun.pic.settings.util.DownExcelUtil;
import org.apache.poi.ss.usermodel.Workbook;
import org.joda.time.LocalDate;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * @Description 干扰源控制器
 * @author Jerry
 * @date 2016-09-18
 */
@Controller
@RequestMapping("/corrctrl/influence")
public class InfluenceController extends BaseController {

    @Resource
    private InfluenceService influenceService;

    //初始化进入高压交流输电系统干扰源页面
    @RequestMapping("/initHtac.html")
    public ModelAndView initHtac(ModelMap model){

        return forword("/corrctrl/cathodic/interferenceSource", model);
    }

    /**
     * 通过类型查询某一种具体干扰源
     * @param influenceVO
     * @param type
     */
    @RequestMapping("/query{type}.json")
    @ResponseBody
    public void queryInfluenceRecode(InfluenceVO influenceVO, @PathVariable("type") String type) {
        InterferenceEnum interferenceEnum = InterferenceEnum.getInstance(type);
        PageJsonBean jsonBean = new PageJsonBean();
        jsonBean.setList(influenceService.queryInfluenceData(influenceVO, interferenceEnum.ordinal()));
        AjaxSupport.sendSuccessText("success","yyyy-MM-dd",jsonBean);
    }


    /**
     * 六大干扰源excel导出
     * @param
     * @author jerry
     */
    @RequestMapping("/export{type}.json")
    @ResponseBody
    public void exportInfluence(HttpServletResponse response, InfluenceVO influenceVO,
                                     @PathVariable("type") String type) throws IOException {
        InterferenceEnum interEnum = InterferenceEnum.getInstance(type);
        int index = interEnum.ordinal();
        String filePath = interEnum.getExpExcelPath();
        List list = influenceService.queryInfluenceData(influenceVO, index);
        List<String[]> listSheet = JXL.toListFormVO(list, "yyyyMMdd");
        Integer beginRow = interEnum.getTempExpIndex();
        Workbook workbook = ExcelUtil.initWorkbook(filePath, listSheet, beginRow - 1);//-2是因为删除了模板前两行
        String excelName = interEnum.getExpName() + LocalDate.now().toString("yyyyMMdd") +".xlsx";
        DownExcelUtil.fillWorkbookHead(workbook);
        WebUtil.setDownHeader(excelName);
        workbook.write(response.getOutputStream());
    }


}
